home *** CD-ROM | disk | FTP | other *** search
- /* Provides a useful variable-length argument error handling abstraction.
-
- Copyright (C) 1989 Free Software Foundation, Inc.
- written by Douglas C. Schmidt (schmidt@ics.uci.edu)
-
- This file is part of GNU GPERF.
-
- GNU GPERF is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- GNU GPERF is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNU GPERF; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
-
- #include "stderr.h"
-
- typedef void (*PTF)(); /* arbitrary pointer to function type */
-
- /* Holds the name of the currently active program. */
-
- static char * program_name;
-
- /***********************************************************************\
- * *
- * name: set_program_name *
- * *
- * descr: Set the name of the current program. *
- * *
- \***********************************************************************/
-
- void set_program_name( char * prog_name )
- {
- program_name = prog_name;
- }
-
-
- /***********************************************************************\
- * *
- * name: report_error *
- * *
- * descr: Report an error using formatted print (same codes as in *
- * printf, with a few extensions). *
- * *
- * %a : exit the program at this point *
- * %e : call the function *
- * %n : print the name of the program *
- * %p : print the errno value *
- * *
- \***********************************************************************/
-
- /* Valid Options (prefixed by '%', as in printf format strings) include:
- 'a': exit the program at this point
- 'c': print a character
- 'd': print a decimal number
- 'e': call the function pointed to by the corresponding argument
- 'f','g': print a double
- 'n': print the name of the program (NULL if not set in constructor or elsewhere)
- 'p': print out the appropriate errno value from syserror( errnno )
- 's': print out a character string
- '%': print out a single percent sign, '%' */
-
- void report_error( char * fmt, ... )
- {
- int sys_nerr = 9999;
- va_list argp;
- int abort = 0;
- char * format;
-
- va_start( argp, fmt );
-
- for ( format=fmt; *format; format++ )
- {
- if ( *format != '%' )
- putc( *format, stderr );
- else
- {
- switch( *++format )
- {
- case 'a' : abort = 1; break;
- case 'c' : putc( va_arg( argp, int ), stderr ); break;
- case 'd' : fprintf( stderr, "%d", va_arg( argp, int ) ); break;
- case 'e' : ( *va_arg( argp, PTF ) )(); break;
- case 'f' : fprintf( stderr, "%g", va_arg( argp, double ) ); break;
- case 'n' : fputs( program_name ? program_name : "error", stderr ); break;
- case 'p' :
- if ( errno < sys_nerr )
- fprintf( stderr, "%s: %s", va_arg( argp, char * ), strerror( errno ) );
- else
- fprintf( stderr, "<unknown error> %d", errno );
- break;
- case 's' : fputs( va_arg( argp, char * ), stderr ); break;
- }
- }
- if ( abort )
- exit( 1 );
- }
- va_end( argp );
- }
-